package com.easyidea.its.web.action.alarm;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Text;
import org.dom4j.tree.DefaultText;

import com.easyidea.its.domain.model.passport.TrackLog;
import com.easyidea.its.domain.model.surveillance.Alarm;
import com.easyidea.its.domain.model.system.PassPort;
import com.easyidea.its.domain.service.surveillance.AlarmService;
import com.easyidea.its.facade.tracklog.TrackLogMgr;
import com.easyidea.its.facade.user.UserMgr;
import com.easyidea.its.util.DateUtil;
import com.easyidea.its.util.PageUtil;
import com.easyidea.its.util.Result;
import com.easyidea.its.util.StringUtil;
import com.easyidea.its.web.action.base.ItsBaseAction;
import com.easyidea.its.web.action.surveillance.dto.SurveillanceDto;
import com.easyidea.its.web.dto.AlarmTrackLogDto;
import com.easyidea.its.web.dto.SeesionUserDto;

/**
 * 
 * @author hukn
 * 
 */
public class AlarmAction extends ItsBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7623025456724886228L;

	private Alarm alarm;
	private AlarmService alarmService;
	private Result<Alarm> alarmResult;
	private SurveillanceDto searchDto;
	private UserMgr userMgr;
	private String content;

	private Integer alarmId;
	private TrackLogMgr trackLogMgr;

	private TrackLog trackLog;
	private String firstPicPath;
	
	private List<AlarmTrackLogDto> alarmTrackLogs;
	
	private Integer width = 400;
	private Integer height = 300;	


	/**
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String alarmList() {
		if (searchDto == null) {
			searchDto = new SurveillanceDto();
			searchDto.setSurveillanceType("1");
			searchDto.setPlateType("0");
		}
		Date now = DateUtil.now();
		Date next = new Date(now.getTime() + 24 * 60 * 60 * 1000);		
		if (searchDto.getStartDate() == null) {
			searchDto.setStartDate(DateUtil.dateToShortStr(now));
			searchDto.setStartHour(0);			
			searchDto.setStartMinute(59);			
		}
		if (searchDto.getEndDate() == null) {
			searchDto.setEndDate(DateUtil.dateToShortStr(next));
			searchDto.setEndHour(23);
			searchDto.setEndMinute(59);
		}		
		int totalRecords = alarmService.getAlarmCountByDto(searchDto);
		Collection<Alarm> newRecords = new ArrayList<Alarm>();
		if (totalRecords > 0) {
			int start = (this.currentPage - 1) * this.getPageSize();
			Collection<Alarm> records = alarmService.getAlarmByDto(searchDto, start, this.getPageSize());
			
			for (Alarm alarm : records ) {
				trackLog = this.trackLogMgr.getLatestTrackLogByCondition(alarm.getPassPortName(),
						alarm.getDirection(), true, null, alarm.getAlarmDate());
				if(trackLog != null) {
					alarm.setRealPlateNumber(trackLog.getPlateNumber())	;
				}
				newRecords.add(alarm);
			}
			alarmResult = PageUtil.createResult(this.getPageSize(), currentPage, totalRecords, newRecords);
		} else {
			alarmResult = PageUtil.createResult(this.getPageSize(), currentPage, 0, newRecords);
		}
		return SUCCESS;
	}

	
	public String realtimeAlarmDetail() {
		alarm = alarmService.getBeanById(alarmId);
		if(alarm != null) {
			trackLog = this.trackLogMgr.getLatestTrackLogByCondition(alarm.getPassPortName(),
					alarm.getDirection(), true, null, alarm.getAlarmDate());
			if (trackLog != null)
			firstPicPath = StringUtil.strEncode(trackLog.getFirstPicPath());
		}

			return SUCCESS;

	}
	public String alarmIframe() {
		return SUCCESS;
	}
	public String realTimeAlarm() {
//		SeesionUserDto dto = (SeesionUserDto) this.getSession().getAttribute(ItsConstants.SESSION_KEY_USER_INFO);
//		dto = userMgr.getUserPassPortsByUserId(dto.getUserId());
//		Document doc;
//		try {
//			doc = createXMLData(dto);
//			content = doc.asXML();
//		} catch (IOException e) {
//		}
		alarmTrackLogs = this.alarmService.getLatestAlarm();
		return SUCCESS;
	}

	private static Document createXMLData(SeesionUserDto dto) throws IOException {
		Document document = DocumentHelper.createDocument();
		document.setXMLEncoding("UTF-8");

		Element root = document.addElement("tree");
		root.addAttribute("id", "-1");

		if (dto != null && dto.getDepartmentName() != null && !dto.getDepartmentName().equals("")) {
			Element item = root.addElement("item");
			item.addAttribute("text", dto.getDepartmentName());
			item.addAttribute("id", "0");
			item.addAttribute("open", "1");
			item.addAttribute("im0", "folderClosed.gif");
			item.addAttribute("im1", "folderOpen.gif");
			item.addAttribute("im2", "folderClosed.gif");
			Element userdata = item.addElement("userdata");
			userdata.addAttribute("name", "url");
			List<PassPort> passPortList = dto.getPassPortList();
			for (PassPort passPort : passPortList) {
				Element subItem = item.addElement("item");
				subItem.addAttribute("text", passPort.getPassPortName());
				subItem.addAttribute("id", passPort.getPassPortName());
				subItem.addAttribute("im0", "folderClosed.gif");
				subItem.addAttribute("im1", "folderOpen.gif");
				subItem.addAttribute("im2", "folderClosed.gif");
				Element subUserdata = subItem.addElement("userdata");
				subUserdata.addAttribute("name", "url");
				Text text = new DefaultText("/its/auth/alarm/portAlarmList.action");
				subUserdata.add(text);
				if (passPort.getStartpoint() != null && passPort.getEndpoint() != null) {
					Element startpoint = subItem.addElement("item");
					startpoint.addAttribute("text", passPort.getStartpoint());
					startpoint.addAttribute("id", passPort.getId().toString() + "_" + passPort.getStartpoint());
					startpoint.addAttribute("im0", "book_titel.gif");
					startpoint.addAttribute("im1", "fbook.gif");
					startpoint.addAttribute("im2", "book_titel.gif");
					Element startUserdata = startpoint.addElement("userdata");
					startUserdata.addAttribute("name", "url");
					Text text1 = new DefaultText("/its/auth/alarm/portAlarmList.action");
					startUserdata.add(text1);
					Element endpoint = subItem.addElement("item");
					endpoint.addAttribute("text", passPort.getEndpoint());
					endpoint.addAttribute("id", passPort.getId().toString() + "_" + passPort.getEndpoint());
					endpoint.addAttribute("im0", "book_titel.gif");
					endpoint.addAttribute("im1", "fbook.gif");
					endpoint.addAttribute("im2", "book_titel.gif");
					Element endtUserdata = endpoint.addElement("userdata");
					endtUserdata.addAttribute("name", "url");
					Text text2 = new DefaultText("/its/auth/alarm/portAlarmList.action");
					endtUserdata.add(text2);
				}

			}

		}
		return document;

	}

	public Alarm getAlarm() {
		return alarm;
	}

	public void setAlarm(Alarm alarm) {
		this.alarm = alarm;
	}

	public AlarmService getAlarmService() {
		return alarmService;
	}

	public void setAlarmService(AlarmService alarmService) {
		this.alarmService = alarmService;
	}

	public Result<Alarm> getAlarmResult() {
		return alarmResult;
	}

	public void setAlarmResult(Result<Alarm> alarmResult) {
		this.alarmResult = alarmResult;
	}

	public SurveillanceDto getSearchDto() {
		return searchDto;
	}

	public void setSearchDto(SurveillanceDto searchDto) {
		this.searchDto = searchDto;
	}

	public UserMgr getUserMgr() {
		return userMgr;
	}

	public void setUserMgr(UserMgr userMgr) {
		this.userMgr = userMgr;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}


	public TrackLogMgr getTrackLogMgr() {
		return trackLogMgr;
	}

	public void setTrackLogMgr(TrackLogMgr trackLogMgr) {
		this.trackLogMgr = trackLogMgr;
	}

	public TrackLog getTrackLog() {
		return trackLog;
	}

	public void setTrackLog(TrackLog trackLog) {
		this.trackLog = trackLog;
	}
	public String getFirstPicPath() {
		return firstPicPath;
	}

	public void setFirstPicPath(String firstPicPath) {
		this.firstPicPath = firstPicPath;
	}

	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}

	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}


	public List<AlarmTrackLogDto> getAlarmTrackLogs() {
		return alarmTrackLogs;
	}


	public void setAlarmTrackLogs(List<AlarmTrackLogDto> alarmTrackLogs) {
		this.alarmTrackLogs = alarmTrackLogs;
	}


	public Integer getAlarmId() {
		return alarmId;
	}


	public void setAlarmId(Integer alarmId) {
		this.alarmId = alarmId;
	}
}
